// pages/authstore/index.js
const {
  GetCate,
  CreatCard,
  CreatMasterCard,
  GetCard,
  UpdateCard,
  UpdateMasterCard,
  GetMemberPhone,
  MemberType
} = require('../../utils/api')
import confg from '../../utils/config';
import util from '../../utils/util';
function showText(str) {
  wx.showToast({
    title: str,
    icon: 'none',
    duration: 2000
  })
}
Page({

  /**
   * 页面的初始数据
   */
  data: {
    confg: confg,
    forms: {},
    catelist: [],
    region: [],
    id: '',
    type: '',
    fenlei: '',
    allcard: [],
    chuchuang: false,
    code: ''
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      type: options.type
    })
    if (options.ids) {
      let arrs = confg.task_type;
      arrs.forEach(el => {
        if (options.ids.indexOf(el.id) >= 0) {
          el['is_c'] = true
        } else {
          el['is_c'] = false
        }
      })
      this.setData({
        'confg.task_type': arrs
      })
    }
    if (options.id) {
      this.setData(options)
      GetCard(options).then(res => {
        let das = options.type == 1 ? res.data : res.data[0] || {};
        this.setData({
          forms: das,
          fenlei: das.platform,
          region: [das.province, das.city, das.area]
        })
        this.getCate(das.domain)
      })
    } else {
      let that = this;
      wx.login({
        success: res => {
          that.data.code = res.code
        }
      })
      util.checkLogin(res => {
        let obj = {
          member: {
            avatar: res.avatar
          }
        }
        if (options.type == 1) {
          obj['name'] = res.name
        } else if (options.type == 2) {
          obj['nature_name'] = res.name;
        }
        this.setData({
          forms: obj
        })
      }, null)
      this.getCate()
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },
  formSubmit(e) {
    let val = e.detail.value;
    let sendobj = {};
    if(!val.addr[0]){
      showText('请选择地区')
      return
    }
    for (let k in val) {
      if (k == 'addr') {
        sendobj['province'] = val[k][0]
        sendobj['city'] = val[k][1]
        sendobj['area'] = val[k][2]
      } else if (k == 'domain') {
        sendobj[k] = val[k].join()
      } else {
        sendobj[k] = val[k];
      }
    }
    this.doFn(sendobj, res => {
      if (res.code == 200) {
        MemberType({
          type: this.data.type
        }).then(ss => {
          wx.showToast({
            title: '创建成功',
            icon: 'success',
            duration: 1500
          })
          wx.navigateBack();
        })
      } else {
        wx.showToast({
          title: res.info,
          icon: 'none',
          duration: 1500
        })
      }
    })
  },
  doFn(obj, cb) {
    if (this.data.id) {
      obj['id'] = this.data.id;
      if (this.data.type == 1) {
        UpdateCard(obj).then(res => {
          wx.navigateBack()
        })
      } else {
        UpdateMasterCard(obj).then(res => {
          wx.navigateBack()
        })
      }
    } else {
      if (this.data.type == 1) {
        CreatCard(obj).then(res => {
          cb(res)
        })
      } else {
        CreatMasterCard(obj).then(res => {
          cb(res)
        })
      }
    }
  },
  formReset() {
    console.log('form发生了reset事件')
  },
  /**
   * 获取产品领域
   */
  getCate(ob) {
    GetCate({
      type: 1
    }).then(res => {
      let data = res.data;
      if (ob && ob.length) {
        ob.forEach(el => {
          data.forEach(els => {
            if (el.id == els.id) {
              els.check = true
            }
          })
        })
      }
      this.setData({
        catelist: data
      })
    })
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  /**
   * 选择地区
   */
  bindRegionChange: function (e) {
    this.setData({
      region: e.detail.value
    })
  },
  /**
   * 获取手机号
   */
  getPhone: function (e) {
    let data = e.detail,
      _this = this;
    if (e.detail.errMsg !== "getPhoneNumber:ok") { return; }
    wx.checkSession({
      success: function (res) {
        if (res.errMsg !== "checkSession:ok") {return;}
        let obj = {
          iv: data.iv,
          encryptedData: data.encryptedData,
          code: _this.data.code
        }
        GetMemberPhone(obj).then(ress => {
          if (ress.code == 200) {
            _this.setData({
              'forms.mobile': ress.data
            })
          }else{
            GetCard().then(rb => {
              let das = _this.data.type == 1 ? rb.data : rb.data[0] || {};
              _this.setData({
                'forms.mobile': das.mobile
              })
            })
          }
        })
      },
      fail: function (res) {
        wx.login({
          success: res => {
            _this.data.code = res.code
          }
        })
      }
    })
  },
  platformFn(e) {
    this.setData({
      fenlei: e.detail.value
    })
  },
  showcaseFn(e) {
    this.setData({
      chuchuang: e.detail.value == 1 ? true : false
    })
  }
})